package com.jzm;



import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy implements MethodInterceptor {


    //前置
    public void before(){
        System.out.println("before........");
    }

    //后置
    public void after(){
        System.out.println("after........");
    }

    //获取代理类实例
    public<T> T getProxy(Class<T> target){
        return (T)Enhancer.create(target,this);
    }


    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        before();

        Object returnObj = proxy.invokeSuper(obj,args);

        after();

        return returnObj;
    }
}
